using System.Collections.Generic;

namespace Engine.FileFormats3D.WR2TrafficFiles
{
    public class LaneFileBlock
    {
        #region Private Fields

        private List<Lane> lanes = new List<Lane>();

        #endregion Private Fields

        #region Public Methods

        public short Count()
        {
            return (short)lanes.Count;
        }

        /// <summary>
        /// Finds the id of the  which has the same properties
        /// </summary>
        /// <param name="numberOfLanes"></param>
        /// <param name="offset1"></param>
        /// <param name="offset2"></param>
        /// <returns></returns>
        public short GetLaneIdOrAddNewOne(Lane lane)// short numberOfLanes, float offset1, float offset2)
        {
            /* Try to find a lane with the given properties */
            for (int i = 0; i < this.lanes.Count; i++)
            {
                if (this.lanes[i].numberofLanes == lane.numberofLanes &&
                    this.lanes[i].Offset1 == lane.Offset1 &&
                    this.lanes[i].Offset2 == lane.Offset2)
                {
                    return (short)i;
                }
            }

            /*  Create a new Lane if not the same lane is found! */
            this.lanes.Add(new Lane(lane.numberofLanes, lane.Offset1, lane.Offset2));
            return (short)(this.lanes.Count - 1);
        }

        public void Save(System.IO.BinaryWriter bw)
        {
            foreach (Lane lane in this.lanes)
            {
                lane.ToStream(bw);
            }
        }

        #endregion Public Methods
    }
}